<?php
class AppController extends Controller {
	var $components = array('Session', 'Auth', 'RequestHandler');
	var $helpers = array('Html', 'Form', 'Time', 'Javascript');

    function beforeFilter() {
		$this->Auth->loginAction = array('prefix' => null, 'controller' => 'users', 'action' => 'login', 'admin' => false);
		$this->Auth->loginRedirect = array('prefix' => null, 'controller' => 'dashboard', 'action' => 'index', 'admin' => false);
		$this->Auth->logoutRedirect = '/';
		$this->Auth->loginError = 'Login Failed.  Please try again';
		$this->Auth->authorize = 'controller';
		$this->Auth->fields = array('username' => 'username', 'password' => 'password');
		$this->Auth->userModel = 'User';

		if (isset($this->params[Configure::read('Routing.admin')])) {
			$this->layout = 'admin';
		}

    }

	function beforeRender() {
		if($this->detectiPhone()) {
			$this->set('iphone', true);
			$this->layout = "iwebkit";
			//$this->layout = "iphone";
			$this->params['action'] = $this->params['action'] . "_iphone";
		} else {
			$this->set('iphone', false);
		}
	}


	// Must be admin to get to admin pages
	function isAuthorized() {
		if (isset($this->params[Configure::read('Routing.admin')])) {
			if($this->Auth->user('admin')){
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}

	protected function detectiPhone() {
		//return strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
		$container = '';
		if(isset($_SERVER['HTTP_USER_AGENT'])) :
		$container = $_SERVER['HTTP_USER_AGENT'];
		endif;
		//print_r($container); //this prints out the user agent array. uncomment to see it shown on page.
		$useragents = array (
			"iPhone","iPod","Android");
		$this->iphone = false;
		foreach ( $useragents as $useragent ) {
			if (eregi($useragent,$container)){
				$this->iphone = true;
			}
		}
		return $this->iphone;
	}

    public function storeSession($session_id)
    {
        $sessionFileHandle = fopen(TMP . DS . $session_id . '.ser.tmp', 'w');
                        
        if ($sessionFileHandle !== false)
        {
            fwrite($sessionFileHandle, serialize($_SESSION));
            fclose($sessionFileHandle);
            
            return true;
        }
        
        return false;
    }

    public function restoreSession($session_id)
    {
        if (preg_match('/^[A-Za-z0-9]*$/', $session_id))
        {
            $sessionFile = TMP . DS . $session_id . '.ser.tmp';
            
            if (@file_exists($sessionFile) && @is_file($sessionFile) && @is_readable($sessionFile) && @filesize($sessionFile) > 0)
            {
                $contents = file_get_contents($sessionFile);
                $oldSession = @unserialize($contents);
                
                if (is_array($oldSession) && count($oldSession) > 0)
                {
                    foreach($oldSession as $id => $value)
                    {
                        $this->Session->write($id, $value);
                    }
                }
                
                @unlink($sessionFile);
                
                return true;
            }
        }
        
        return false;
    }


}
?>